package com.studentstreet.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.studentstreet.R;
import com.studentstreet.entities.CartEntity;
import com.studentstreet.tools.HtmlTools;
import com.studentstreet.tools.dbhelper.OrderDBHelper;

/**
 * @author likebamboo
 * 
 */
public class OrderAdapter extends BaseAdapter {
	private Context context = null;
	private List<CartEntity> mModel = new ArrayList<CartEntity>();

	public OrderAdapter(Context context, String userName) {
		super();
		this.context = context;
		OrderDBHelper dbHelper = new OrderDBHelper(context);
		mModel = dbHelper.getCart(userName);
	}

	@Override
	public int getCount() {
		return mModel.size();
	}

	@Override
	public Object getItem(int position) {
		return mModel.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		if (view == null) {
			view = LayoutInflater.from(context).inflate(
					R.layout.order_list_item, null);
		}
		CartEntity cart = mModel.get(position);
		// 大神经验：不可将这些TextView等控件声明为类成员，否则他们的监听器
		// 都会被最后一个Item的相应对象捕获
		TextView cartNameText = (TextView) view
				.findViewById(R.id.order_goods_name);
		TextView cartPriceText = (TextView) view
				.findViewById(R.id.order_goods_price);
		EditText cartCountEdit = (EditText) view
				.findViewById(R.id.order_goods_count);
		// 设置商品名
		cartNameText.setText(HtmlTools.getGoods(cart.getCart_name(),
				cart.getCart_sellerName()));
		// 设置商品价格
		cartPriceText.setText(HtmlTools.getPrice(context, cart.getCart_price()
				+ ""));
		// 设置商品数量
		cartCountEdit.setText(cart.getCart_count() + "");
		return view;
	}

}
